"""
Case Type   : SQL语法
Case Name   : alter table truncate 不加update global index,查询索引状态
Create At   : 2023/04/19
Owner       : cr13
Description :
    1.创建表并插入数据
    2.为表创建全局索引
    3.查询索引状态
    4.alter table truncate不加update global index
    5.查询索引状态
    6.查询表数据
    7.清理环境
Expect      :
    1.成功
    2.成功
    3.索引有效
    4.成功
    5.索引失效
    6.对应被truncate分区数据为空
    7.成功
History     :
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.commsh = CommonSH('PrimaryDbUser')
        self.tb_name = 't_ddl_truncate_014'
        self.index = 'i_ddl_truncate_014'

    def test_truncate(self):
        text = "----step1:创建表并插入数据 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name};" \
            f"create table {self.tb_name}(id int,name text) partition by " \
            f"range(id) " \
            f"(" \
            f"  partition p1 values less than (10)," \
            f"  partition p2 values less than (20)," \
            f"  partition p3 values less than (maxvalue));" \
            f"insert into {self.tb_name} values (generate_series(1,30)," \
            f"'test');"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败'
                      + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, result, '执行失败'
                      + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result, '执行失败'
                      + text)

        text = "----step2:为表创建全局索引 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop index if exists {self.index};" \
            f"create index {self.index} on {self.tb_name} using btree(id) " \
            f"global;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_INDEX_SUCCESS_MSG, result, '执行失败'
                      + text)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, result,
                      '执行失败' + text)

        text = "----step3:查询索引状态 expect:索引有效----"
        self.log.info(text)
        sql_cmd = f"\\d+ {self.tb_name}"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        flg = str(result.splitlines()[6]).strip()
        self.assertEqual(f'"{self.index}" btree (id) TABLESPACE pg_default',
                         flg, '执行失败' + text)

        text = "----step4:alter table truncate 不加update global index " \
               "expect:成功----"
        self.log.info(text)
        sql_cmd = f"alter table {self.tb_name} truncate partition p1;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.ALTER_TABLE_MSG, result, '执行失败' + text)

        text = "----step5:查询索引状态 expect:索引失效----"
        self.log.info(text)
        sql_cmd = f"\\d+ {self.tb_name}"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(f'"{self.index}" btree (id) TABLESPACE pg_default '
                      f'UNUSABLE', result, '执行失败' + text)

        text = "----step6:查询表数据 expect:对应被truncate分区数据为空----"
        self.log.info(text)
        sql_cmd = f"select * from {self.tb_name} where id <10;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertEqual(str(result.splitlines()[-1]).strip(), '(0 rows)',
                         '执行失败' + text)

    def tearDown(self):
        text = '-----step7:清理环境 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name} cascade;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败'
                      + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
